package bookshop.controller.front;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import bookshop.domain.Browsing;
import bookshop.domain.Customer;
import bookshop.service.BookService;
import bookshop.service.BrowsingHistoryService;
import bookshop.service.CategoryService;
import bookshop.service.CustCommentService;
import bookshop.service.OrderService;
import bookshop.service.helper.BookQueryHelper;
import bookshop.service.helper.CustCommentQueryHelper;
import bookshop.service.impl.BookServiceImpl;
import bookshop.service.impl.BrowsingHistoryServiceImpl;
import bookshop.service.impl.CategoryServiceImpl;
import bookshop.service.impl.CustCommentServiceImpl;
import bookshop.service.impl.OrderServiceImpl;
import bookshop.utils.Page;

public class WorkflowServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");

		String act = request.getParameter("act");
		if ("mainscreen".equals(act)) {

			int id = 12;

			Page page = new Page();
			BookQueryHelper helper = new BookQueryHelper();
			CategoryService ctgService = new CategoryServiceImpl();
			BookService bookService = new BookServiceImpl();
			
			
			OrderService orderService = new OrderServiceImpl();
			request.setAttribute("sales", orderService.GetSaleBook());
			
			
			helper.setQryBookCtg(id);
			request.setAttribute("ctgs", ctgService.loadall());
			request.setAttribute("currentCtg", ctgService.getCategoryById(id));
			request.setAttribute("page",
					bookService.loadBooksByCondition(helper, page));

			request.getRequestDispatcher("jsps/front/main_screen.jsp").forward(
					request, response);
		} else if (("showcover").equals(act)) {
			String bookCode = request.getParameter("bookcode");

			BookService bookService = new BookServiceImpl();
			byte[] image = bookService.loadCover(bookCode);

			if (image == null || image.length == 0) {
				// 去磁盘上读取，默认图书封面，并提交给浏览器显示。该图片为默认图片。
				// 图片的路径：
				// 1. WEB路径
				// ：http://localhost:8080/bookshop/pics/backend/no_book_cover.jpg
				// 2. 磁盘真实路径：D:\j2eedev\Workspaces\MyEclipse
				// 10\.metadata\.me_tcat\webapps\bookshop\pics/backend/no_book_cover.jpg

				// String fileName=request.getRealPath("/")
				// 把web根路径所对应的磁盘物理真实路径，返回回来。
				String fileName = request.getRealPath("/")
						+ "pics/backend/no_book_cover.jpg";
				File file = new File(fileName);
				FileInputStream fis = new FileInputStream(file);
				image = new byte[fis.available()];
				fis.read(image);
			}

			response.setContentType("image/jpeg");
			ServletOutputStream sos = response.getOutputStream();
			sos.write(image);
			sos.flush();
			sos.close();

		} else if ("showBooksByCtg".equals(act)) {

			int ctgId = Integer.parseInt(request.getParameter("ctgId"));

			BookQueryHelper helper = new BookQueryHelper();
			helper.setQryBookCtg(ctgId);
			if (request.getParameter("qryBookCode") != null
					&& request.getParameter("qryBookCode").length() != 0)
				helper.setQryBookCode(request.getParameter("qryBookCode"));

			if (request.getParameter("qryBookPubYear") != null
					&& request.getParameter("qryBookPubYear").length() != 0)
				helper.setQryBookCode(request.getParameter("qryBookPubYear"));

			if (request.getParameter("qryBookName") != null
					&& request.getParameter("qryBookName").length() != 0)
				helper.setQryBookName(request.getParameter("qryBookName"));

			if (request.getParameter("qryBookPublisher") != null
					&& request.getParameter("qryBookPublisher").length() != 0)
				helper.setQryBookPublisher(request
						.getParameter("qryBookPublisher"));

			if (request.getParameter("qryBookPubYearLeft") != null
					&& request.getParameter("qryBookPubYearLeft").length() != 0)
				helper.setQryBookPubYearLeft(request
						.getParameter("qryBookPubYearLeft"));

			if (request.getParameter("qryBookPubYearRight") != null
					&& request.getParameter("qryBookPubYearRight").length() != 0)
				helper.setQryBookPubYearRight(request
						.getParameter("qryBookPubYearRight"));

			if (request.getParameter("qryBookPriceLeft") != null
					&& request.getParameter("qryBookPriceLeft").length() != 0)
				helper.setQryBookPriceLeft(Double.parseDouble(request
						.getParameter("qryBookPriceLeft")));

			if (request.getParameter("qryBookPriceRight") != null
					&& request.getParameter("qryBookPriceRight").length() != 0)
				helper.setQryBookPriceRight(Double.parseDouble(request
						.getParameter("qryBookPriceRight")));

			if (request.getParameter("MarkSequence") != null)
				helper.setBookMarkSequence(request.getParameter("MarkSequence"));

			if (request.getParameter("PriceSequence") != null)
				helper.setBookPriceSequence(request
						.getParameter("PriceSequence"));

			Page page = new Page();
			page.setPageSize(6);
			if (request.getParameter("pageNo") != null)
				page.setPageNo(Integer.parseInt(request.getParameter("pageNo")));

			BookService bookService = new BookServiceImpl();
			CategoryService ctgs = new CategoryServiceImpl();
			CategoryService ctgService = new CategoryServiceImpl();

			request.setAttribute("page",
					bookService.loadBooksByCondition(helper, page));
			request.setAttribute("ctgs", ctgService.loadall());
			request.setAttribute("currentCtg",
					ctgService.getCategoryById(ctgId));
			
			OrderService orderService = new OrderServiceImpl();
			request.setAttribute("sales", orderService.GetSaleBook());

			request.getRequestDispatcher("jsps/front/main_screen.jsp").forward(
					request, response);

		} else if ("showBookByCode".equals(act)) {
			String bookCode = request.getParameter("bookCode");
            Page page = new Page();
            CustCommentQueryHelper helper = new CustCommentQueryHelper();
            helper.setBookCode(bookCode);
            CustCommentService	custCommentService = new CustCommentServiceImpl();		
			request.setAttribute("page", custCommentService.loadCommentsByCondition(helper, page));
            
			BookService bookService = new BookServiceImpl();
			Customer customer = (Customer)request.getSession().getAttribute("loginedCustomer");
			String sessionId = request.getSession().getId();
			Browsing brows = new Browsing();
			brows.setBook(bookService.getBookByCode(bookCode));
			brows.setLogTime(new java.util.Date());
			brows.setSessionId(sessionId);
			if(customer!=null){
			brows.setCustId(customer.getCustomerId());
			}
			BrowsingHistoryService bhService = new BrowsingHistoryServiceImpl();
			bhService.savaBrowsHistory(brows);
			
			request.setAttribute("browss", bhService.getBrowssBySessionId(sessionId));
			request.setAttribute("books", bhService.getBookByBrows(bookCode,sessionId));
			
			request.setAttribute("book", bookService.getBookByCode(bookCode));
			request.getRequestDispatcher("jsps/front/show_book.jsp").forward(
					request, response);

		}

	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doGet(request, response);
	}

}
